
package com.kj.tms.system.controller.role;


import com.kj.tms.common.bean.dto.ReturnData;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.pri.system.menu.service.ModuleService;
import com.kj.tms.pri.system.role.dto.RoleDto;
import com.kj.tms.pri.system.role.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * Created by zhangsong on 2017/10/19.
 * <p>
 * 角色管理模块
 */
@Controller
@RequestMapping("/system/role")
public class RoleController {
    private static Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RoleService roleServiceImpl;
    @Autowired
    private ModuleService moduleServiceImpl;

    /**
     * @Title: index
     * @Description: 首页
     */
    @RequestMapping("/index")
    public String index() throws Exception {
        return "system/role/index";
    }

    /**
     * @Title: toAdd
     * @Description: 跳转到新增模块
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "system/role/add";
    }

    /**
     * @Title: toUpdate
     * @Description: 跳转到更新模块
     */
    @RequestMapping("/toUpdate")
    public String toUpdate() {
        return "system/role/update";
    }

    /**
     * @param model
     * @return void 返回类型
     * @throws Exception
     * @Title: queryByPage
     * @Description: 查询分页数据
     */
    @RequestMapping("/listByPage")
    @ResponseBody
    public ReturnData listByPage(RoleDto model) {
        Map<String, Object> returnObject = roleServiceImpl.listByPage(model);
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }

    /**
     * @param dto
     * @return void 返回类型
     * @throws Exception
     * @Title: save
     * @Description: 添加或修改数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public ReturnData save(RoleDto dto) {
        return roleServiceImpl.save(dto);
    }

    /**
     * @param roleId
     * @param validMark
     * @return void 返回类型
     * @throws Exception
     * @Title: updateValidMark
     * @Description: 启用/停用
     */
    @RequestMapping("/updateValidMark")
    @ResponseBody
    public ReturnData updateValidMark(@RequestParam(value = "roleId",required = false) String roleId,
                                      @RequestParam(value = "validMark",required = false) String validMark,
                                      HttpServletResponse response){
        RoleDto dto = new RoleDto();
        dto.setRoleId(roleId);
        if (("0").equals(validMark)) {
            dto.setValidMark(Consts.VALID_MARK_1);
        } else {
            dto.setValidMark(Consts.VALID_MARK_0);
        }
        try{
            roleServiceImpl.updateBySelective(dto);
        }catch (Exception e){
            logger.error(e.getMessage());
            return new ReturnData(Consts.ERROR, "停/启用失败！", null);
        }

        return new ReturnData(Consts.SUCCESS, "停/启用成功！", null);
    }

    /**
     * @param roleId
     * @param @throws Exception
     * @return void 返回类型
     * @throws Exception
     * @Title: del--delmark
     * @Description: V1.0.0.0 des查询角色是否有用户信息，
     * 有：禁止删除；次之判断是否已分配权限，有 禁止删除。
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ReturnData delete(@RequestParam(value = "roleId",required = false) String roleId) {
        return roleServiceImpl.delete(roleId);
    }

    /**
     * @param roleId
     * @return void 返回类型
     * @throws Exception
     * @Title: getTreeData
     * @Description: 查询菜单模块树
     */
    @RequestMapping("/getTreeData")
    @ResponseBody
    public ReturnData getTreeData(@RequestParam(value = "roleId",required = false) String roleId) {
        return new ReturnData(Consts.SUCCESS, "success", moduleServiceImpl.getTreeData(roleId));
    }

    /**
     * @param model
     * @param menuCodes
     * @return void 返回类型
     * @throws Exception
     * @Title: queryByList
     * @Description: 查询列表数据
     */
    @RequestMapping("/batchAddMenu")
    @ResponseBody
    public ReturnData listAddMenu(RoleDto model,
                                  @RequestParam(value = "menuCodes",required = false) String menuCodes) {
        return roleServiceImpl.listAddMenu(model, menuCodes);
    }


}

